$(function(){
	$('.product-img-thumbs li').hover(function(){
		$('.product-img-thumbs li').removeClass('selected');
		$(this).addClass('selected');
		src = $(this).find('img').attr('src');
		$('.product-img-curr img').attr('src', src);
		$('.product-img-curr img').attr('jqimg', src);
	});
	
	$(".jqzoom").jqueryzoom({
		xzoom : 300,
		yzoom : 300,
		offset : 5,
		position : "right",
		lens : 1,
		preload : 1
	});
	
	$('.tab-bar li a').click(function(){
		self = $(this);
		self.closest('ul').find('a').removeClass('selected');
		self.addClass('selected');
		panel = self.attr('href');
		$('.tab-panel').removeClass('active');
		$(panel).addClass('active');
		event.preventDefault();
		return;
	});
	
	var comments_pageno = 1,level=1, comments = $('.comments .bd'), comment_page = $('.comments .page');
	
	function getComments(){
		url = comment_page.attr('data-url');
		paramsters = {p: comments_pageno,level:level};
		comments.html('');
		comment_page.html('');
		$.get(url, paramsters, function(data){
			comment_page.append(data.pages);
			
			$.each(data.results, function(i, item){
				message = '';
//				$.each(item.messages, function(ms_i, ms_item){
//					if (ms_item.is_saler == 1) {
//						message += '<p class="saler">解释：' + ms_item.message + '</p>';
//					} else {
//						message += '<p>' + ms_item.message + '</p>';
//					}
//				});
				
				htmlstring = '<li>';
				htmlstring += '<div class="info">';
				htmlstring += '<span>';
				htmlstring += '<p>'+item.content+'</p>';
				if(item.recomment){
					$.each(item.recomment,function(k,kitem){
						htmlstring += '<p class="saler">商家回复：'+kitem+'</p>';
					});
				}		
				
				htmlstring += '</span>';
				htmlstring += '<span>'+item.nickname+'</span>';
				htmlstring += '<span>'+item.format_time+'</span>';
				htmlstring += '</div>';
				htmlstring += '<ul class="img">';
				if(item.images){				
				$.each(item.images,function(k,kitem){
					htmlstring += '<li><a href="javascript:;" class="popimg"><img src="'+kitem+'" /></a></li>';
					
				});				
				}
				htmlstring += '</ul>';
				htmlstring += '</li>';
				
				comments.append(htmlstring);
			});
		}, "json");
	}
	
	$(document).delegate(".comments .page a", "click", function(){
		self = $(this);
		comments_pageno = self.attr('data-page');
		getComments();
	});
	
	$(document).delegate(".comment_level input", "click", function(){
		self = $(this);
		comments_pageno =$(".comments .page a").attr('data-page');
		level=self.val();
		getComments();
	});
	
	getComments();
	
	addtocart();
	
});

function changequantity(addon) {
	_quantity = $('#quantity');
	quantity = $('#quantity').val();
	
	
	if (addon == '+') {
		quantity++;
	} else if (addon == '-') {
		quantity--;
	} else {
		return;
	}
	
	if (quantity < 1) {
		quantity = 1;
	}	
	_quantity.val(quantity);
	$('input[name=quantity]').val(quantity);
}

//加入购物车
function addtocart(){
	$('.cart').click(function(){
		/*
		var self=$(this);
		var quantity=$('quantity').val();
		var url=self.attr('data-url');
		var product_id=self.attr('data-bind');		
		var myform=$('#formaddcart');
		
		myform.action = url+"quantity"+quantity + "product_id"+product_id;
		myform.submit();
		*/
		var thisform=$(this).closest('form');
		var quantity=thisform.find('input[name=quantity]').val();
		
		if(parseInt(quantity)<1){
			updateAlert('购买的数量有误');
			return false;	
		}
		else{
			$(this).closest('form').submit();
		}
	});
}